#include "widget.h"
#include "ui_widget.h"

#include <QMessageBox>
#include <QDialog>
#include <QLabel>

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{


    ui->setupUi(this);
    //创建登录框
    QDialog dlg;

    // dlg.setWindowFlag(Qt::FramelessWindowHint);


    dlg.setWindowFlag(Qt::FramelessWindowHint);
    dlg.setStyleSheet("Widget{margin:0}");


    QLabel *lbl_title = new QLabel("服务器登录");
    lbl_title->setStyleSheet("QLabel{color:white;font-size:12px;min-height:30px;background-color:green;}");
    lbl_title->setAlignment(Qt::AlignHCenter|Qt::AlignVCenter);
    QLabel *lbl_host = new QLabel("服务器:");
    QLabel *lbl_name = new QLabel("用户名:");
    QLabel *lbl_password = new QLabel("密码:");

    QLineEdit *le_host = new QLineEdit("yhdn.cn");
    QLineEdit *le_name = new QLineEdit("admin");
    QLineEdit *le_password = new QLineEdit("Stallman7*");
    le_password->setEchoMode(QLineEdit::Password);
    QPushButton *btn_ok = new QPushButton("OK");
    QPushButton *btn_exit = new QPushButton("EXIT");



    QGridLayout *gl = new QGridLayout;
    QHBoxLayout *h1 = new QHBoxLayout;
    QHBoxLayout *h2 = new QHBoxLayout;
    QVBoxLayout *v1 = new QVBoxLayout;

    h1->addWidget(lbl_title);

    gl->addWidget(lbl_host,0,0);
    gl->addWidget(le_host,0,1);
    gl->addWidget(lbl_name,1,0);
    gl->addWidget(le_name,1,1);
    gl->addWidget(lbl_password,2,0);
    gl->addWidget(le_password,2,1);
    h2->addWidget(btn_ok);
    h2->addStretch();
    h2->addWidget(btn_exit);
    gl->addLayout(h2,3,1);

    v1->addLayout(h1);
    v1->addLayout(gl);
    gl->setContentsMargins(5,5,5,10);
    v1->setContentsMargins(0,0,0,0);

    dlg.setLayout(v1);
    dlg.setWindowIcon(QIcon(":/images/qt-logo.png"));
    dlg.setWindowTitle("系统登录");
    dlg.show();

    //信号和槽
    connect(btn_exit,SIGNAL(clicked()),&dlg,SLOT(reject()));
    connect(btn_ok,&QPushButton::clicked,this,[&](){

        //新建数据库表格
        db = QSqlDatabase::addDatabase("QMYSQL");
        db.setHostName(le_host->text());
        db.setUserName(le_name->text());
        db.setPassword(le_password->text());
        db.setDatabaseName("world");
        if(!db.open()){
            QMessageBox::information(this,"error",db.lastError().text());
        }
        else {
            dlg.accept();
        }

    });

    auto dialogCode = dlg.exec();

    if (dialogCode == QDialog::Rejected)
    {
        qDebug() << "reject";

        this->deleteLater();
    }
    else
    {

        this->close();


    }





    //初始化数据
    m_le_map = {
                {"cpu",ui->m_le_cpu},
                {"mem",ui->m_le_mem},
                {"mb",ui->m_le_mb},
                {"vga",ui->m_le_vga},
                {"ssd",ui->m_le_ssd},
                {"hdd",ui->m_le_hdd},
                };



    /*
CREATE TABLE mydatabase.mytable (
    id INT AUTO_INCREMENT PRIMARY KEY,
    name VARCHAR(50),
    details JSON
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

*/

    QSqlQuery query;
    QString sql = "CREATE TABLE IF NOT EXISTS dn"
                  "(id INT AUTO_INCREMENT PRIMARY KEY,"
                  "name VARCHAR(50),"
                  "details JSON)"
                  "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;";
    bool ret = query.exec(sql);
    if (!ret)
        qDebug() << query.lastError().text();


}

Widget::~Widget()
{
    delete ui;
    db.close();
}

void Widget::on_m_btn_submit_clicked()
{
    //将数据打包成json对象
    QJsonObject jsonObj,jsonObj2;

    foreach (const auto &item, m_le_map.keys()) {
        jsonObj[item] = m_le_map[item]->text();
    }


    jsonObj2["name"] = ui->m_le_consumer_name->text();
    jsonObj2["address"] = ui->m_le_consumer_address->text();
    jsonObj2["tel"] = ui->m_le_consumer_tel->text();
    jsonObj["consumer"] = jsonObj2;

     QString details = QJsonDocument(jsonObj).toJson(QJsonDocument::Compact);

    //插入到数据库中
    QSqlQuery query;
    query.prepare("INSERT INTO dn (name,details) VALUES(:name,:details);");
    query.bindValue(":name",ui->m_le_consumer_name->text());
    query.bindValue(":details",details);
    bool ret = query.exec();
    if (!ret)
        QMessageBox::information(this,"银河电脑","数据插入失败"+query.lastError().text());
    else
        QMessageBox::information(this,"银河电脑","数据插入成功");

}

